package com.timi_licai.timi_licai_account.fragment;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.timi_licai.timi_licai_account.MyConstants;
import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.database.DatabaseUtils;
import com.timi_licai.timi_licai_account.listener.KeyLineInterface;
import com.timi_licai.timi_licai_account.utils.DateUtils;
import com.timi_licai.timi_licai_account.utils.KeyLineUtils;
import com.timi_licai.timi_licai_account.view.LineView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class ShowKeyLineFragment extends Fragment implements View.OnClickListener {
    public static final String ARG_PAGE = "page";
    private KeyLineInterface keyLineInterface;
    private LineView lineView;

    private int mPageNumber;

    private RadioButton rb0;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    private RadioButton rb5;
    private RadioButton rb6;
    private List<Date> dates;

    public ShowKeyLineFragment() {
    }


    /**
     * 调用此方法可以创建一个Fragment
     *
     * @param pageNumber
     * @return
     */
    public static Fragment create(int pageNumber) {
        ShowKeyLineFragment fragment = new ShowKeyLineFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, pageNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPageNumber = getArguments().getInt(ARG_PAGE);
        Calendar calendar = KeyLineUtils.getSelectCalendar(mPageNumber);
        Log.d("^^", "calendar-->" + DateUtils.parseDate(calendar.getTime()));
        dates = KeyLineUtils.getDates(calendar);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_show_key_line, container, false);
        rb0 = (RadioButton) view.findViewById(R.id.zero);
        rb1 = (RadioButton) view.findViewById(R.id.one);
        rb2 = (RadioButton) view.findViewById(R.id.two);
        rb3 = (RadioButton) view.findViewById(R.id.three);
        rb4 = (RadioButton) view.findViewById(R.id.four);
        rb5 = (RadioButton) view.findViewById(R.id.five);
        rb6 = (RadioButton) view.findViewById(R.id.six);
        rb0.setOnClickListener(this);
        rb1.setOnClickListener(this);
        rb2.setOnClickListener(this);
        rb3.setOnClickListener(this);
        rb4.setOnClickListener(this);
        rb5.setOnClickListener(this);
        rb6.setOnClickListener(this);
        lineView = (LineView) view.findViewById(R.id.lineView);
        List<String> list = new LinkedList<>();
        List<String> dateList = new LinkedList<>();
        for (int i = 0; i < dates.size(); i++) {
            Date date = dates.get(i);
            SimpleDateFormat format = new SimpleDateFormat("dd");
            String s = format.format(date);
            list.add(s);
            String formatDate = parseDateToDataBase(dates.get(i));
            dateList.add(formatDate);
        }
        rb0.setText(list.get(0));
        rb1.setText(list.get(1));
        rb2.setText(list.get(2));
        rb3.setText(list.get(3));
        rb4.setText(list.get(4));
        rb5.setText(list.get(5));
        rb6.setText(list.get(6));
        String[] projection = new String[]{"fee"};
        List<Float> feeList = new LinkedList<>();
        for (int i = 0; i < dateList.size(); i++) {
            float fee = 0.0f;
            String[] selectionArgs = new String[]{dateList.get(i)};
            Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_ACCOUNT_BOOK_DETAILS, projection, "createtime=?", selectionArgs, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    fee += cursor.getFloat(cursor.getColumnIndex("fee"));
                }
            }
            feeList.add(fee);
        }
        flush(feeList);
        return view;
    }

    private String parseDateToDataBase(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String formatDate = dateFormat.format(date);
        return formatDate;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        Date date = null;
        switch (id) {
            case R.id.zero:
                date = dates.get(0);
                break;
            case R.id.one:
                date = dates.get(1);
                break;
            case R.id.two:
                date = dates.get(2);
                break;
            case R.id.three:
                date = dates.get(3);
                break;
            case R.id.four:
                date = dates.get(4);
                break;
            case R.id.five:
                date = dates.get(5);
                break;
            case R.id.six:
                date = dates.get(6);
                break;
        }

        if (keyLineInterface != null) {
            keyLineInterface.getKeyLineDate(date);
        }
    }

    public void flush(List<Float> list) {
        lineView.flushData(list);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof KeyLineInterface) {
            keyLineInterface = (KeyLineInterface) activity;
        }
    }
}
